package cn.hxzy.common.Eeception;

import cn.hxzy.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice(basePackages = "cn.hxzy.service.impl")
public class GlobalExceptionHandler {
    //全局异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.error().message("执行了全局异常处理...");
    }

    //特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.error().message("执行了ArithmeticException异常处理...");
    }

    //自定义异常
    @ExceptionHandler(DiyException.class)
    @ResponseBody
    public Result error(DiyException e){
        e.printStackTrace();
        return Result.error().code(e.getCode()).message(e.getName());
    }

}
